package com.qire.manhua.model.viewModel.impl;

import com.qire.common.collect.ListUtil;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.AccountPurchaseHistoryController;
import com.qire.manhua.dal.service.AccountPurchaseHistoryService;
import com.qire.manhua.databinding.LayoutPurchaseHistoryItemBinding;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;

public class AccountPurchaseHistoryViewModel extends SmartRefreshViewModel implements IViewModel {

    private AccountPurchaseHistoryService historyService = new AccountPurchaseHistoryService(this);

    private final ArrayList<PurchaseHistoryItemViewModel> purchaseHistoryList = new ArrayList<>();
    private final SummerRecyclerBindAdapter purchaseHistoryAdapter = new SummerRecyclerBindAdapter(purchaseHistoryList);

    private int pageNo = 1;

    @Override
    public int getVariableId() {
        return BR.accountPurchaseHistoryViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     * @param controller 事件控制器
     */
    public void onAddition(AccountPurchaseHistoryController controller) {
        purchaseHistoryAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        this.historyService = null;
        this.purchaseHistoryAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin
    public void resetPage() {
        pageNo = 1;
        clearPurchaseHistoryList();
    }

    public void nextPage() {
        pageNo++;
    }

    public void loadData() {
        resetPage();
        loadMoreData();
    }

    public void loadMoreData() {
        historyService.fetchPurchaseHistoryData(pageNo);
    }

    private void clearPurchaseHistoryList() {
        purchaseHistoryAdapter.notifyItemRangeRemoved(0, purchaseHistoryList.size());
        purchaseHistoryList.clear();
    }
    public void addPurchaseHistoryItem(int comicId, String comicName, String comicCover, CharSequence coinUnLockInfo,
                                       CharSequence beanUnLockInfo, CharSequence updateInfo) {
        purchaseHistoryList.add(new PurchaseHistoryItemViewModel(comicId, comicName, comicCover, coinUnLockInfo, beanUnLockInfo, updateInfo));
    }
    public void refreshPurchaseHistoryAdapter() {
        purchaseHistoryAdapter.notifyItemChanged(0, "full");
    }

    //endregion #业务数据 end

    //region    #UI操作 begin

    public void refreshEmptyDataPanel() {
        boolean isEmpty = ListUtil.isEmpty(purchaseHistoryList);
        set("dataPanelNo", isEmpty ? 0 : 1);
    }

    //endregion #UI操作 end

    //region    #UI绑定 begin
    public MutableLiveData<Integer> getDataPanelNo() {
        return get("dataPanelNo");
    }

    public Adapter getPurchaseHistoryAdapter() {
        return purchaseHistoryAdapter;
    }

    public LayoutManager getPurchaseHistoryLayoutManager() {
        return new LinearLayoutManager(null);
    }
    //endregion #UI绑定 end


    @AdapterEntityBind(viewRid = R.layout.layout_purchase_history_item)
    public class PurchaseHistoryItemViewModel extends BaseObservable implements AutoVariable<LayoutPurchaseHistoryItemBinding> {

        public final int comicId;
        public final String comicName;
        public final String comicCover;
        public final CharSequence coinUnLockInfo;
        public final CharSequence beanUnLockInfo;
        public final CharSequence updateInfo;

        public PurchaseHistoryItemViewModel(int comicId, String comicName, String comicCover, CharSequence coinUnLockInfo,
                                            CharSequence beanUnLockInfo, CharSequence updateInfo) {
            this.comicId = comicId;
            this.comicName = comicName;
            this.comicCover = comicCover;
            this.coinUnLockInfo = coinUnLockInfo;
            this.beanUnLockInfo = beanUnLockInfo;
            this.updateInfo = updateInfo;
        }

        @Override
        public void bindVariable(LayoutPurchaseHistoryItemBinding viewBind) {
            viewBind.setPurchaseHistoryItemViewModel(this);
        }

    }

}
